import logging
from typing import Optional

import PySide6.QtWidgets
from PySide6.QtCore import QEvent, Qt
from PySide6.QtWidgets import QSplitter, QWidget
from ....PSData import *
import PySide6.QtCore


class CollapsibleSplitter(QSplitter, PSDataServer):
    def __init__(self, parent: 'QWidget' = None):
        super().__init__(parent = parent)
        self.setOrientation(Qt.Horizontal)
        self._explandWidth = dict()
        self.setContentsMargins(0, 0, 0, 0)
        
    def CollapseWidget(self, index: int) -> None:
        if (self.count() > index):
            self._explandWidth[index] = self.widget(index).width()
            self.moveSplitter(0, index + 1)


    def ExpandWidget(self, index: int) -> None:
        if (self.count() > index):
            self.moveSplitter(self._explandWidth[index], index + 1)

    def childEvent(self, arg__1: PySide6.QtCore.QChildEvent) -> None:
        if (arg__1.type() == QEvent.ChildAdded):
            # logging.debug('QEvent.ChildAdded')
            # logging.debug(self.count())
            if (self.count() > 1):
                # logging.debug('childevent')
                self.setStretchFactor(0, 0)
                self.setStretchFactor(1, 1)
